﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Models;
using DAL;

namespace 包覆门.DealerManager
{
    public partial class DealerEditing : Form
    {
        public DealerEditing()
        {
            InitializeComponent();
        }
        public string EditingId { get; set; }
        public bool IsNew { get; set; }
        private DealerDAL dal = new DealerDAL();
        //加载
        private void DealerEditing_Load(object sender, EventArgs e)
        {
            if (IsNew) { }
            else
            {
                DealerInfo d = dal.GetById(EditingId);
                this.tbdname.Text = d.Dname;
                this.tbdphone.Text = d.Dphone;
                this.tbdaddress.Text = d.Daddress;
                this.tbcontact.Text = d.Dcontact;
            }
        }
        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
           
            if (!ValidateData()) return;
             DealerInfo d = new DealerInfo();
            d.Dname=this.tbdname.Text.Trim();
            d.Dphone=this.tbdphone.Text.Trim();
            d.Daddress=this.tbdaddress.Text.Trim();
            d.Dcontact = this.tbcontact.Text.Trim();
            if (IsNew)
            {
                int result = dal.SaveData(d);
                if (result == 1)
                {
                    MessageBox.Show("保存成功");
                    DialogResult = DialogResult.OK;
                }
                else {
                    MessageBox.Show("保存失败");
                    DialogResult = DialogResult.No;
                }  
            }
            else 
            {
                d.Id = Convert.ToInt32(EditingId);
                int result = dal.UpdateData(d);
                if (result == 1)
                {
                    MessageBox.Show("保存成功");
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("保存失败");
                    DialogResult = DialogResult.No;
                }  
            }


        }
        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.No;
        }
        //验证
        public bool ValidateData() {
            bool flag = true;
            if (string.IsNullOrWhiteSpace(this.tbdname.Text)) {
                flag = false;
                MessageBox.Show("经销商名称不能为空");
            }
            if (string.IsNullOrWhiteSpace(this.tbdphone.Text))
            {
                flag = false;
                MessageBox.Show("电话不能为空");
            }
            if (string.IsNullOrWhiteSpace(this.tbcontact.Text))
            {
                flag = false;
                MessageBox.Show("收货人不能为空");
            }
            

            return flag;
        }
    }
}
